package com.uvlab;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class JxApplication
{
    private  JxAppService svc=null;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(new Object[]{JxApplication.class}, args);

    }

    @PostConstruct
    public void init(){
        System.out.println("JxApplication.init() ...");
//        this.scanner = new JxAppBootScanner();
//         assert(svc != null);
//         svc.init(this.scanner);
    }
    @PreDestroy
    public void  dostory(){
        System.out.println("JxApplication cleanup...");
    }

    @Bean
    public JxAppService svc()
    {
        if (svc == null) {
            System.out.println("JxApplication.svc(): creating the JxAppService component.");
            this.svc = new JxAppService();
        }
        return svc;
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                // registry.addMapping("/rest").allowedOrigins("http://localhost:9000");
                // registry.addMapping("/**").allowedOrigins("http://localhost");
                registry.addMapping("/**");
            }
        };
    }
}
